package com.feng.collection;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class LinkedListDemo {

	public static void main(String[] args) {
		LinkedList<Object> list = new LinkedList<>();
		list.add(null);//LinkedList可存null值
		System.err.println(list.size());//1
		list.add(1);
		list.add(2);
		list.add(3);
		list.add(4);
//		list.removeFirst();
		System.out.println(list);//[null, 1, 2, 3, 4]
		list.remove();//删除第一个元素
		System.out.println(list);//[1, 2, 3, 4]
		Object remove = list.remove(3);
		System.err.println(remove);//4
		System.out.println(list);//删除索引位3的元素[1, 2, 3]
		
		//队列操作FIFO-先进先出
		Object peek = list.peek();//得到第一个
		Object poll = list.poll();//删除第一个元素并返回该值
		list.offer(5);//添加到最后
		System.out.println(list);//[2, 3, 5]
		
		// Deque operations--双端队列操作
		/**
		 * public boolean offerFirst(E e)--队首添加
		 * public boolean offerLast(E e)--队尾添加
		 * public E peekFirst()--得到队首值
		 * public E peekLast()--得到队尾值
		 * public E pollFirst()--删除队首并返回该值
		 * public E pollLast()--删除队尾并返回该值
		 * public void push(E e)--队首添加
		 * public E pop()--队尾删除
		 */
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		List<Integer> list2=new LinkedList<>();
		for (int i = 1; i <= 10; i++) {
			list2.add(i);
		}
		System.err.println(list2);
		for (Iterator<Integer> iterator=list2.iterator();iterator.hasNext();) {
			int i = iterator.next();
			if (i%2==0) {
				System.out.println(i);
				iterator.remove();
			}
		}
		System.out.println(list2);
		
		System.err.println("--------------------------------------");
		List<Integer> currentList=new CopyOnWriteArrayList<>();
		for (int i = 1; i <= 10; i++) {
			currentList.add(i);
		}
		System.out.println(currentList);
		for (Integer i : currentList) {
			if (i%2==0) {
				currentList.remove(i);
				System.out.println(currentList);
			}
		}
		System.err.println(currentList);
		
	}
}
